home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / osi / isode / dosisode / DOSISODE80.ZIP / ISODE8.WRK / UNIX / LIB / DNS / PROTOENT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-16  |  2.8 KB  |  122 lines

  1. #include <fiddle.h>
  2. #define _PATH_PROTOCOLS "/etc/protocols"
  3. /*
  4.  * Copyright (c) 1983 Regents of the University of California.
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms are permitted
  8.  * provided that: (1) source distributions retain this entire copyright
  9.  * notice and comment, and (2) distributions including binaries display
  10.  * the following acknowledgement:  ``This product includes software
  11.  * developed by the University of California, Berkeley and its contributors''
  12.  * in the documentation or other materials provided with the distribution
  13.  * and in all advertising materials mentioning features or use of this
  14.  * software. Neither the name of the University nor the names of its
  15.  * contributors may be used to endorse or promote products derived
  16.  * from this software without specific prior written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  19.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21.  
  22. #if defined(LIBC_SCCS) && !defined(lint)
  23. static char sccsid[] = "@(#)getprotoent.c    5.7 (Berkeley) 6/1/90";
  24. #endif /* LIBC_SCCS and not lint */
  25.  
  26. #include <stdio.h>
  27. #include <sys/types.h>
  28. #include <sys/socket.h>
  29. #include <netdb.h>
  30. #include <ctype.h>
  31.  
  32. #define    MAXALIASES    35
  33.  
  34. static FILE *protof = NULL;
  35. static char line[BUFSIZ+1];
  36. static struct protoent proto;
  37. static char *proto_aliases[MAXALIASES];
  38. static char *any();
  39. int _proto_stayopen;
  40.  
  41. setprotoent(f)
  42.     int f;
  43. {
  44.     if (protof == NULL)
  45.         protof = fopen(_PATH_PROTOCOLS, "r" );
  46.     else
  47.         rewind(protof);
  48.     _proto_stayopen |= f;
  49. }
  50.  
  51. endprotoent()
  52. {
  53.     if (protof) {
  54.         fclose(protof);
  55.         protof = NULL;
  56.     }
  57.     _proto_stayopen = 0;
  58. }
  59.  
  60. struct protoent *
  61. getprotoent()
  62. {
  63.     char *p;
  64.     register char *cp, **q;
  65.  
  66.     if (protof == NULL && (protof = fopen(_PATH_PROTOCOLS, "r" )) == NULL)
  67.         return (NULL);
  68. again:
  69.     if ((p = fgets(line, BUFSIZ, protof)) == NULL)
  70.         return (NULL);
  71.     if (*p == '#')
  72.         goto again;
  73.     cp = any(p, "#\n");
  74.     if (cp == NULL)
  75.         goto again;
  76.     *cp = '\0';
  77.     proto.p_name = p;
  78.     cp = any(p, " \t");
  79.     if (cp == NULL)
  80.         goto again;
  81.     *cp++ = '\0';
  82.     while (*cp == ' ' || *cp == '\t')
  83.         cp++;
  84.     p = any(cp, " \t");
  85.     if (p != NULL)
  86.         *p++ = '\0';
  87.     proto.p_proto = atoi(cp);
  88.     q = proto.p_aliases = proto_aliases;
  89.     if (p != NULL) {
  90.         cp = p;
  91.         while (cp && *cp) {
  92.             if (*cp == ' ' || *cp == '\t') {
  93.                 cp++;
  94.                 continue;
  95.             }
  96.             if (q < &proto_aliases[MAXALIASES - 1])
  97.                 *q++ = cp;
  98.             cp = any(cp, " \t");
  99.             if (cp != NULL)
  100.                 *cp++ = '\0';
  101.         }
  102.     }
  103.     *q = NULL;
  104.     return (&proto);
  105. }
  106.  
  107. static char *
  108. any(cp, match)
  109.     register char *cp;
  110.     char *match;
  111. {
  112.     register char *mp, c;
  113.  
  114.     while (c = *cp) {
  115.         for (mp = match; *mp; mp++)
  116.             if (*mp == c)
  117.                 return (cp);
  118.         cp++;
  119.     }
  120.     return ((char *)0);
  121. }
  122.